package iosr11.gbase.dao;

import iosr11.data.Teacher;
import iosr11.data.User;

import java.util.List;
import java.util.logging.Logger;

import com.google.api.gbase.client.FeedURLFactory;
import com.google.api.gbase.client.GoogleBaseEntry;
import com.google.api.gbase.client.GoogleBaseFeed;
import com.google.api.gbase.client.GoogleBaseQuery;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.contacts.ContactEntry;

public class TeacherDAOImpl extends GenericDAOImpl<Teacher> implements TeacherDAO {

	private static String TYPE = "Teacher";
	private static String NAME_LECTURER = "is lecturer";
	private static String NAME_USER_REF = "user";
	private Logger log = Logger.getLogger("service-log");
	
	@Override
	public Teacher getByLogin(String login) { /*nieaktualne*/
		ContactEntry user = DAOLocatorImpl.getInstance().getContactsDAO().getUserOld(login);
		String userId = user.getId();
		GoogleBaseQuery query = new GoogleBaseQuery(FeedURLFactory.getDefault().getItemsFeedURL());
		GoogleBaseFeed feed = DAOLocatorImpl.getInstance().getGoogleBaseQueryResult(query, NAME_USER_REF, userId);
		if (feed == null) {
			log.info("brak wpisow w bazie o polu typu string o nazwie: "+NAME_USER_REF+" i wartosci "+login);
			return null;
		}
		feed = DAOLocatorImpl.getInstance().selectByType(feed, TYPE);
		if (feed == null) {
			log.info("brak wpisow okreslajacych nauczyciela dla loginu: "+login);
			return null;
		}
		if (feed.getEntries().size()!=1) {
			log.warning("ilosc wpisow okreslajacych nauczyciela jest niepoprawna (rozna od 1) dla loginu: "+login);
			return null;
		}
		
		GoogleBaseEntry entry = feed.getEntries().get(0);
		Teacher teacher = new Teacher();
		teacher.setId(entry.getId());
		teacher.setName(user.getTitle().getPlainText());
		teacher.setEmail(user.getEmailAddresses().get(0).getAddress());
		
		/* brak obslugi grup - grupy tylko przy studentach */
		teacher.setGroups(null);
		
		teacher.setLecturer(entry.getGoogleBaseAttributes().getBooleanAttribute(NAME_LECTURER));
		return teacher;
	}

	@Override
	public Teacher getByUserRef(String ref) {
		Teacher teacher = new Teacher();
		GoogleBaseQuery query = new GoogleBaseQuery(FeedURLFactory.getDefault().getItemsFeedURL());
		GoogleBaseFeed feed = DAOLocatorImpl.getInstance().getGoogleBaseQueryResult(query, NAME_USER_REF, ref);
		if (feed == null) {
			log.info("brak rekordow dla referencji: "+ref);
			return null;
		}
		feed = DAOLocatorImpl.getInstance().selectByType(feed, TYPE);
		if (feed == null) {
			log.info("brak rekordow okreslajacych nauczyciela dla ref: "+ref);
			return null;
		}
		if (feed.getEntries().size()!=1) {
			log.warning("ilosc wpisow okreslajacych nauczyciela jest niepoprawna (rozna od 1) dla ref: "+ref);
			return null;
		}
		teacher.setLecturer(feed.getEntries().get(0).getGoogleBaseAttributes().getBooleanAttribute(NAME_LECTURER));
		teacher.setId(feed.getEntries().get(0).getId());
		return teacher;
	}

	@Override
	public Class<Teacher> getEntityClass() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Teacher rewrite(GoogleBaseEntry entry) {
		Teacher teacher = new Teacher();
		if (entry.getGoogleBaseAttributes().getBooleanAttribute(NAME_LECTURER)==null) return null;
		teacher.setLecturer(entry.getGoogleBaseAttributes().getBooleanAttribute(NAME_LECTURER));
		teacher.setId(entry.getId());
		User user = new User();
		user.setId(entry.getGoogleBaseAttributes().getTextAttribute(NAME_USER_REF));
		teacher.setUser(user);
		return teacher;
	}

	@Override
	public GoogleBaseEntry rewrite(Teacher entity) {
		GoogleBaseEntry entry = new GoogleBaseEntry();
		entry.setTitle(new PlainTextConstruct("teacher"));
		entry.setDraft(true);
		entry.getGoogleBaseAttributes().setItemType(TYPE);
		entry.getGoogleBaseAttributes().addBooleanAttribute(NAME_LECTURER, entity.isLecturer());
		entry.getGoogleBaseAttributes().addTextAttribute(NAME_USER_REF, entity.getUser().getId());
		return entry;
	}

	@Override
	public boolean checkType(GoogleBaseEntry entry) {
		return entry.getGoogleBaseAttributes().getItemType().equals(TYPE);
	}

	
}
